package fel.via.cz.thewolfofwallstreet;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpClientStack;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class LoginActivity extends Activity {

    static public RequestQueue requestQueue;
    private EditText etNick;
    private EditText etPass;
    private DefaultHttpClient mHttpClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Activity activity = this;
        mHttpClient = new DefaultHttpClient();
        requestQueue = Volley.newRequestQueue(this, new HttpClientStack(mHttpClient));
        etNick = (EditText) activity.findViewById(R.id.tNickname);
        etPass = (EditText) activity.findViewById(R.id.tPassword);
    }

    public void loginClick(View view) {
        String nick = etNick.getText().toString();
        String pass = etPass.getText().toString();
        if (nick.equals("") && pass.equals("")) return;
        loginRequest(nick, pass);
    }

    private void loginRequest(final String nick, String pass) {
        final String url = MainActivity.serverAddress + "wolfofwallstreet/logon";
        final Context context = this;

        final JSONObject object = new JSONObject();
        try {
            object.put("user", nick);
            object.put("pass", pass);
        } catch (JSONException e) {
            Log.e("LoginActivity ", url);
        }

        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("Response", response.toString());
                        try {
                            if(!response.getBoolean("response")) return;
                            // Nastartuju si tu druhou aktivitu..
                            Intent intent = new Intent(context, MainActivity.class);
                            intent.putExtra("USER_NAME", nick);
                            startActivity(intent);
                        } catch (JSONException e) {
                            Log.e("LoginActivity login exception", e.getMessage());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Error.Response", error.toString());
                    }
                }
        ) {
            @Override
            public HashMap<String, String> getParams() {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("Accept", "application/json");
                params.put("Content-Type", "application/json");
                return params;
            }

            @Override
            public byte[] getBody() {
                return object.toString().getBytes();
            }
        };
        requestQueue.add(postRequest);
    }
}
